var $;
layui.define(['jquery', 'element', 'layer', 'form','utils'], function (exports) {
    $ = layui.$,
        element = layui.element,
        form = layui.form,
        URL = layui.utils.URL,
        layer = layui.layer;

    layer.config({
        zIndex: 10000
    });
    window.winWidth = $(window).width();
    window.heiHeght = $(window).height();

    form.verify({
        mobile: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (value == '') {
                var title = $(item).data('title') || '该项';
                return title + "不能为空";
            }
            var preg = /^[1][3,4,5,7,8][0-9]{9}$/;
            if (!preg.test(value)) {
                return '手机号码格式错误';
            }
        }
        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        , password: [
            /^[\S]{6,20}$/
            , '密码必须6到12位，且不能出现空格'
        ]
        , repassword: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (value == '') {
                return '确认密码不能为空';
            }
            var field = $(item).data('password') || 'password';
            var password = $("#" + field).val();
            if (password != value) {
                return '两次输入密码不一致';
            }
        }
    });

    $(document).on('click', '.new_tab', function (ev) {
        /**
         * 创建新的Tab
         */
        var icon = $(this).data('icon');
        $(this).find("i").attr('data-icon', icon);
        if (parent && parent.Tab) {
            parent.Tab.tabAdd($(this));
        } else {
            Tab.tabAdd($(this));
        }
        if (ev && ev.preventDefault) {
            ev.preventDefault();
        } else {
            window.event.returnValue = false;
        }
        return false;
    }).on('click', '.javascript', function (ev) {
        /**
         * 需要调用javascript事件处理
         */
        var callback;
        if (callback = $(this).attr('rel')) {
            if (window[callback]) {
                window[callback].call(this);
            } else {
                if (callback == 'list_search_switch') {
                    list_search_switch(ev.currentTarget);
                } else if (callback == 'modifyPassword') {
                    modifyPassword(ev.currentTarget);
                } else if (callback == 'modifyUserinfo') {
                    modifyUserinfo(ev.currentTarget);
                } else if (callback == 'setAddress') {
                    setAddress(ev.currentTarget);
                }
            }
        }
        if (ev && ev.preventDefault) ev.preventDefault(); else window.event.returnValue = false;
        return false;
    }).on('click', '.J-item-delete', function (ev) {
        /**
         * 删除条目时
         * @href
         * @data-id
         * @data-confirm
         * @data-title
         */
        var url = $(this).prop('href');
        var id = $(this).data('id');
        var confirm = $(this).data('confirm') || '删除后不能恢复，确定要删除吗?';
        var title = $(this).data('title') || '删除确认';

        if (parent.layer) {
            var Layer = parent.layer;
        } else {
            var Layer = layer;
        }
        Layer.confirm(confirm, {
            title: title,
            resize: false,
            btn: ['立即删除', '再考虑一下'],
            btnAlign: 'c',
            anim: 1,
            icon: 3
        }, function () {
            $.ajax({
                url: url,
                dataType: 'json',
                type: 'post',
                data: {id: id, '_method': 'DELETE'},
                success: function (res) {
                    if (res.code == 1) {
                        Layer.msg('删除成功', {icon: 6, times: 3000}, function () {
                            document.location.reload();
                        })
                    } else {
                        Layer.msg(res.message, {icon: 5, times: 3000});
                    }
                }
            })
        })
        if (ev && ev.preventDefault) ev.preventDefault(); else window.event.returnValue = false;
        return false;
    });

    /**
     * 列表页面搜索框显示/隐藏
     * @param obj
     */
    function list_search_switch(obj) {
        var $this = $(obj);
        var search_box = $this.data('search-box');
        var $list_search = $this.siblings("#" + search_box);
        if ($list_search.is(':animated')) return;
        if ($list_search.is(':hidden')) {
            $list_search.slideDown(0, function () {
                $this.html('关闭搜索');
            })
        } else {
            $list_search.slideUp(0, function () {
                $this.html('展开搜索');
            })
        }
    }

    /**
     * 资料修改
     *
     * @param obj
     */
    function modifyUserinfo(obj) {
        var url = $(obj).attr('href');
        var current_document = document;
        var Layer = layer;
        Layer.open({
            type: 2,
            title: '资料修改',
            content: url,
            resize: false,
            area: ['600px', '500px'],
            btn: ['确定修改', '取消'],
            yes: function (index, layero) {
                var contentWindow = $(layero).find('iframe')[0].contentWindow;
                var document = contentWindow.document;
                contentWindow.doSubmit(function () {
                    Layer.closeAll();
                });
            }
        })
    }
    function setAddress(obj){
        var url = $(obj).attr('href');
        var is_max = $(obj).data('maxmin');
        var maxmin = false;

        var current_document = document;
        if (parent.layer) {
            var Layer = parent.layer;
        } else {
            var Layer = layer;
        }
        var _layer_id = Layer.open({
            type: 2,
            title: '地图坐标提取器',
            content: url,
            resize: false,
            maxmin:true,
            area: ['950px', '550px'],
            btn: ['确定修改', '取消'],
            yes: function (index, layero) {
                var contentWindow = $(layero).find('iframe')[0].contentWindow;
                var document = contentWindow.document;
                contentWindow.doSave(function (longitude,latitude,address) {
                    $("#map_latitude").val(latitude);
                    $("#map_longitude").val(longitude);
                    $("#address").val(address);
                    Layer.closeAll();
                });
            }
        })
        if(is_max!=''){
            Layer.full(_layer_id);
        }
    }
    /**
     * 修改密码
     *
     * @param obj
     */
    function modifyPassword(obj) {
        var url = $(obj).attr('href');
        var current_document = document;
        if (parent.layer) {
            var Layer = parent.layer;
        } else {
            var Layer = layer;
        }
        Layer.open({
            type: 2,
            title: '密码修改',
            content: url,
            resize: false,
            area: ['500px', '350px'],
            btn: ['确定修改', '取消'],
            yes: function (index, layero) {
                var contentWindow = $(layero).find('iframe')[0].contentWindow;
                var document = contentWindow.document;
                contentWindow.doSubmit(index, function (url) {
                    current_document.location.href = url;
                    Layer.closeAll();
                });
            }
        })
    }

    $('.tooltip').hover(function () {
        var text = $(this).attr('data-tip-text');
        var type = $(this).attr('data-tip-type') ? $(this).attr('data-tip-type') : 2;
        var bg = $(this).attr('data-tip-bg') ? $(this).attr('data-tip-bg') : '#393D49';
        if (text) {
            layer.tips(text, $(this), {
                tips: [type, bg],
                time: 0
            });
        }
    }, function () {
        layer.close(layer.tips());
    })

    form.on('submit(save)', function (data) {
        var url = $(data.form).prop('action');
        var data = data.field;
        var redirect = $(data.form).attr('data-redirect') || '';
        URL.post(url, data, {
            dataType: 'json',
            success: function (res) {
                if (res.code == 1) {
                    layer.msg(res.message, {icon: 6, times: 2000}, function () {
                        if (res.data.redirect != "") {
                            document.location.href = res.data.redirect;
                        } else if (redirect != "") {
                            document.location.href = redirect;
                        } else {
                            document.location.reload();
                        }

                    })
                } else {
                    layer.msg(res.message, {icon: 5, times: 2000});
                }
            }
        })
        return false;
    })

    exports('global', {});
});